<template>
	<view class="detail-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="发起会议"></uni-navbar></view>

		<view class="tab-nav">
			<view class="nav-item" :class="navItem == 1 ? 'active' : ''">基本信息和人员</view>
			<view class="nav-item" :class="navItem == 2 ? 'active' : ''">会议安排和内容</view>
		</view>

		<view class="form-box">
			<u-form :model="form" ref="uForm">
				<block v-if="navItem == 1">
					<u-form-item class="form-item" required label="标题" prop="title">
						<u-input v-model="form.title" type="text" placeholder="请输入标题" />
					</u-form-item>
					<u-form-item class="form-item" required label="类别" prop="meetingType">
						<u-input v-model="meetingType" type="select" :select-open="meetingTypeShow" placeholder="请选择类别" @click="meetingTypeShow = true" />
						<u-select :list="meetingTypeList" v-model="meetingTypeShow" @confirm="selectConfirm($event, 'meetingType')"></u-select>
					</u-form-item>
					<u-form-item class="form-item" required label="报名截止" prop="regDeadline">
						<date-time-picker placeholder="请选择报名截止时间" fields="minute" @change="timeChange($event, 'regDeadline')"></date-time-picker>
					</u-form-item>
					<u-form-item class="form-item" required label="开始时间" prop="startTime">
						<date-time-picker placeholder="请选择开始时间" fields="minute" @change="timeChange($event, 'startTime')"></date-time-picker>
					</u-form-item>
					<u-form-item class="form-item" required label="结束时间" prop="endTime">
						<date-time-picker placeholder="请选择结束时间" fields="minute" @change="timeChange($event, 'endTime')"></date-time-picker>
					</u-form-item>
					<u-form-item class="form-item" label="组织部门">
						<u-input v-model="deptId" type="select" :select-open="deptIdShow" placeholder="请选择组织部门" @click="deptIdShow = true" />
						<u-select :list="deptIdList" v-model="deptIdShow" @confirm="selectConfirm($event, 'deptId')"></u-select>
					</u-form-item>
					<u-form-item class="form-item" label="参加代表">
						<u-input v-model="wyName" type="select" :select-open="wyShow" placeholder="请选择参加代表" @click="wyShow = true" />
						<select-tree ref="wytree" :show="wyShow" :range="wyList" @confirm="treeConfirm($event, 'wy')" @cancel="treeCancel($event, 'wy')"></select-tree>
					</u-form-item>
					<u-form-item class="form-item" label="工作人员">
						<u-input v-model="gzName" type="select" :select-open="gzShow" placeholder="请选择工作人员" @click="gzShow = true" />
						<select-tree ref="gztree" :show="gzShow" :range="gzList" @confirm="treeConfirm($event, 'gz')" @cancel="treeCancel($event, 'gz')"></select-tree>
					</u-form-item>
				</block>
				<block v-else>
					<u-form-item class="form-item" label="短信通知"><u-switch v-model="form.smsFlag"></u-switch></u-form-item>
					<u-form-item class="form-item" label="系统通知"><u-switch v-model="form.pushFlag"></u-switch></u-form-item>
					<u-form-item class="form-item" label="是否用餐"><u-switch v-model="form.dinnerFlag"></u-switch></u-form-item>
					<u-form-item class="form-item" label="是否住宿"><u-switch v-model="form.hotelFlag"></u-switch></u-form-item>
					<u-form-item class="form-item" label="是否用车"><u-switch v-model="form.carFlag"></u-switch></u-form-item>

					<u-form-item class="form-item" label="活动安排">
						<u-input v-model="form.contentArrange" type="textarea" placeholder="请输入活动安排" :height="50" :auto-height="true" />
					</u-form-item>
				</block>
			</u-form>
		</view>

		<view class="fiexd-btm">
			<u-button v-if="navItem == 1" type="error" @click="confirm">下一步</u-button>
			<u-button v-else type="error" @click="confirm">提交</u-button>
		</view>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import selectTree from '@/components/uni-select-tree.vue';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker,
		selectTree
	},
	data() {
		return {
			navItem: 1,
			meetingType: '',
			meetingTypeShow: false,
			meetingTypeList: [],
			deptId: '',
			deptIdShow: false,
			deptIdList: [],

			wyIds: '',
			wyName: '',
			wyShow: false,
			wyList: [],

			gzIds: '',
			gzName: '',
			gzShow: false,
			gzList: [],
			form: {
				title: '',
				meetingType: '',
				regDeadline: '',
				startTime: '',
				endTime: '',
				deptId: '',
				pushFlag: true,
				smsFlag: true,
				dinnerFlag: true,
				hotelFlag: true,
				carFlag: true,
				contentArrange: ''
			},
			rules: {
				title: [
					{
						required: true,
						message: '请输入标题',
						trigger: 'change'
					}
				],
				type: [
					{
						required: true,
						message: '请选择类别',
						trigger: 'change'
					}
				],
				regDeadline: [
					{
						required: true,
						message: '请选择报名截止时间',
						trigger: 'change'
					}
				],
				startTime: [
					{
						required: true,
						message: '请选择开始时间',
						trigger: 'change'
					}
				],
				endTime: [
					{
						required: true,
						message: '请选择结束时间',
						trigger: 'change'
					}
				]
			}
		};
	},
	onLoad() {
		this.loadLogin();
		this.loadDict('hylx', 'meetingTypeList');
		this.loadTeam('weiyuan, changwei');
		this.loadDept();
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		treeConfirm(e, type) {
			if (!e || e.length == 0) {
				this[type + 'Show'] = false;
				this[type + 'Name'] = '';
				this[type + 'Ids'] = [];
				return false;
			}
			this[type + 'Ids'] = [];
			let names = [];
			let i = 0;
			e.forEach((item) => {
				this[type + 'Ids'].push(item.id);
				if (i < 3) {
					names.push(item.name);
					i++;
				}
			});
			this[type + 'Name'] = names.join(',') + '等' + e.length + '人';
			this[type + 'Show'] = false;
		},
		treeCancel(e, type) {
			this[type + 'Show'] = false;
		},
		timeChange({ date, dt }, idx) {
			this.form[idx] = date.year + '-' + date.month2 + '-' + date.day2 + ' ' + date.hour2 + ':' + date.minute2 + ':' + '00';
		},
		selectConfirm(e, idx) {
			this[idx] = e[0].label;
			this.form[idx] = e[0].value;
		},
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;
			} else {
				this.$logout();
			}
		},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this[list] = [];
				res.data.forEach((item) => {
					this[list].push({
						value: item.dictValue,
						label: item.dictName
					});
				});
			});
		},
		loadDept() {
			this.$request('/system/sys-dept/depts', {
				clientCode: this.clientCode
			}).then((res) => {
				res.data.forEach((item) => {
					this.deptIdList.push({
						label: item.deptName,
						value: item.id
					});
				});
			});
		},
		loadTeam(type) {
			this.$request('/system/sys-user/team_users', {
				clientCode: this.clientCode,
				currentUserId: this.userId,
				dictType: 'jiebie',
				roleCodes: type,
				teamType: '2',
				withCw: '1'
			}).then((res) => {
				this.wyList = res.data;
				this.gzList = res.data;
			});
		},
		confirm() {
			if (this.navItem == 1) {
				this.$refs.uForm.validate((valid) => {
					if (!valid) {
						return false;
					} else {
						this.navItem = 2;
					}
				});
			} else {
				let data = {};
				this.form.pushFlag = !this.form.pushFlag ? '0' : '1';
				this.form.smsFlag = !this.form.smsFlag ? '0' : '1';
				this.form.dinnerFlag = !this.form.dinnerFlag ? '0' : '1';
				this.form.hotelFlag = !this.form.hotelFlag ? '0' : '1';
				this.form.carFlag = !this.form.carFlag ? '0' : '1';
				this.form.clientCode = this.clientCode;

				data = {
					hyhdMeeting: this.form,
					gz_ids: this.gzIds,
					wy_ids: this.wyIds
				};
				this.$request('/huiyihuodong/hyhd-meeting/add', data, {
					load: true
				}).then((res) => {
					this.$msg(res.msg);
					setTimeout(() => {
						let pre = this.$prePage();
						if (pre && pre.loadHAll) {
							pre.loadHAll();
						}
						uni.navigateBack();
					}, 500);
				});
			}
		}
	}
};
</script>

<style lang="scss">
.detail-view {
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid $u-border-color;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
